/**
 * 〈菜单管理控制层〉
 * @author LiJunYi
 * @create 2019/7/7
 * @since 1.0.0
 */
package com.ljy.system.marvel_sys.system.controller;

import com.ljy.system.marvel_sys.system.model.permission.Permission;
import com.ljy.system.marvel_sys.system.service.IPermissionService;
import com.ljy.system.marvel_sys.system.service.IRolePermissionService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("permission")
public class PermissionController {

    @Autowired
    IPermissionService permissionService;
    @Autowired
    IRolePermissionService rolePermissionService;

    @RequestMapping("list")
    @ResponseBody
    public List<Permission> listPermission(){
        return permissionService.listPermission();
    }

    /**新增目录-菜单*/
    @RequestMapping("insert")
    @ResponseBody
    public int insertPermission(Permission permission){
        return permissionService.insertPermission(permission);
    }

    /**跟新目录-菜单*/
    @RequestMapping("update")
    @ResponseBody
    public int updatePermission(Permission permission){
        return permissionService.updatePermission(permission);
    }

    /**删除菜单或者根目录
     * 删除菜单需判断是否已经分配出去，已分配无法删除
     * 目录也是一样（目录暂未开启删除功能）
     * */
    @RequestMapping("delete/{id}")
    @ResponseBody
    @RequiresRoles("admin")
    public String delPermission(@PathVariable("id") int id){
        /*判断该菜单是否存在角色菜单表中*/
       String flag = rolePermissionService.queryByPermissionId(id);
       if("no".equals(flag)){
           String msg = permissionService.deletePermission(id);
           return msg;
       }else{
           return flag;
       }
    }
}
